package com.um.jdy.mp.user.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.um.jdy.common.platform.dao.MerchantDao;
import com.um.jdy.common.platform.entity.po.Merchant;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 邓南辉
 * 日期 2023/6/1
 * 版权所有 广州优匠科技有限公司
 */
@Service
@Transactional
@Slf4j
public class SessionUserServiceImpl implements SessionUserService {

    @Autowired
    MerchantDao merchantDao;

    @Override
    public Merchant getTokenUser() {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        if (!tokenInfo.isLogin) {
            throw new ServiceException(CommonCode.LoginExpired);
        } else {
            Merchant merchant = merchantDao.getEntity(tokenInfo.getLoginId().toString());
            if (null == merchant) {
                throw new ServiceException(CommonCode.LoginExpired);
            } else {
                return merchant;
            }
        }
    }


    @Override
    public String getTokenUserId() {
        Merchant user = this.getTokenUser();
        if (null == user) {
            return "";
        }
        return StringUtil.getTrim(user.getPlatformMerchantId());
    }

    /**
     * 查询用户ID主键名称
     *
     * @return
     */
    @Override
    public String getUserIdName() {
        return "platform_merchant_id";
    }


}
